#include "stdafx.h"
#include "scanline.h"
#include "drawing.h"


scanline16 * scanline16_create()
{
	scanline16 * pscl = new scanline16;
	pscl->y = 0;
	pscl->index = 0;
	pscl->spans = 0;
	pscl->count = 0;
	pscl->last = 0;
	return pscl;
}
void scanline16_destroy(scanline16 * pscl)
{
	for(int_16 cnt = 0; cnt != pscl->index; ++cnt)
		span16_destroy(pscl->spans + cnt);
	delete[] pscl->spans;
	delete pscl;
}

span16 * scanline16_get_last_span(scanline16 * pscl, int_16 x)
{
	if(pscl->last && pscl->last->x == x)
		return pscl->last;
	else
	{
		if(pscl->index == pscl->count)
		{
			int_16 count = pscl->count + 256;
			span16 * psps = new span16[count];
			memcpy_s(psps, count * sizeof(span16), pscl->spans, pscl->index * sizeof(span16));
			
			for(int_16 cnt = 0; cnt != pscl->count; ++cnt)
				span16_destroy(pscl->spans + cnt);
			delete[] pscl->spans;
			pscl->spans = psps;
			pscl->count = count;
		}

		span16 * psp = pscl->spans + (pscl->index ++);
		psp->x = x;
		return psp;
	}
}
void scanline16_add_pixel(scanline16 * pscl, int_16 x, int_16 y, uint_8 cover)
{
	span16 * psp = scanline16_get_last_span(pscl, x);
	span16_add(psp, cover);
}